package com.sychip.fhc.di

import android.content.Context
import android.widget.Toast
import com.fhc.base.GlobalCoroutineExceptionHandler
import com.sychip.fhc.MainApplication
import com.sychip.fhc.app.pref.PrefDataStoreIf
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent
import dagger.hilt.android.components.ViewModelComponent
import dagger.hilt.android.qualifiers.ActivityContext
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber


//全局单例作用域（SingletonComponent）
//Activity作用域（ActivityComponent）
//ViewModel作用域（ViewModelComponent）
@Module
@InstallIn(ViewModelComponent::class)
object ViewModelModule {

    @Provides
    fun provideApplication(@ApplicationContext context: Context): MainApplication {
        return context as MainApplication
    }
}


@Module
@InstallIn(ActivityComponent::class) // 限定为Activity作用域
object ActivityModule {
    @Provides
    fun provideActivityDataManager(
        dataStore: PrefDataStoreIf,
        @ActivityContext context: Context
    ): AcitivtyDataManager {
        return AcitivtyDataManager(dataStore, context)
    }
}


@Module
@InstallIn(SingletonComponent::class)
object ApplicationModule {
    @Provides
    fun provideAppDataManager(
        dataStore: PrefDataStoreIf,
        @ApplicationContext context: Context
    ): AppDataManager {
        return AppDataManager(dataStore, context)
    }

    @Provides
    fun provideCoroutineExceptionHandler(): CoroutineExceptionHandler {
        return GlobalCoroutineExceptionHandler()
    }
}


class AppDataManager(
    private val dataStore: PrefDataStoreIf,
    private val activity: Context
) {
    fun saveThemeMode(scope: CoroutineScope? = null, isDark: Boolean) {
        if(scope != null) scope.launch {
            Timber.Forest.tag("saveThemeMode").i("saveThemeMode   ${ dataStore.getAppType()}")
        }else{
//            Timber.Forest.tag("saveThemeMode").i("saveThemeMode   ${ dataStore.getAppType()}")
        }
    }

    fun showToast(scope: CoroutineScope? = null, message: String) {
        if(scope != null) scope.launch {
            withContext(Dispatchers.Main) {
                Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
            }
        }else{
            Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
        }
    }
}


class AcitivtyDataManager(
    private val dataStore: PrefDataStoreIf,
    private val activity: Context
) {
    fun saveThemeMode(scope: CoroutineScope? = null, isDark: Boolean) {
        if(scope != null) scope.launch {
            Timber.Forest.tag("saveThemeMode").i("saveThemeMode   ${ dataStore.getAppType()}")
        }else{
//            Timber.Forest.tag("saveThemeMode").i("saveThemeMode   ${ dataStore.getAppType()}")
        }
    }

    fun showToast(scope: CoroutineScope? = null, message: String) {
        if(scope != null) scope.launch {
            withContext(Dispatchers.Main) {
                Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
            }
        }else{
            Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
        }
    }
}